package com.palfund.animationandanimator;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.view.animation.AccelerateDecelerateInterpolator;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.AnimationSet;
import android.view.animation.AnimationUtils;
import android.view.animation.RotateAnimation;
import android.view.animation.ScaleAnimation;
import android.view.animation.TranslateAnimation;
import android.widget.ImageView;

public class TweenActivity extends AppCompatActivity {
    private ImageView mImageView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_tween);
        mImageView = (ImageView) findViewById(R.id.imageView);
    }
    public void click(View view) {
        Animation animation = null;
        switch (view.getId()) {
            case R.id.btn_alpha_xml:
                mImageView.startAnimation(AnimationUtils.loadAnimation(this, R.anim.alpha));
                return;
            case R.id.btn_rotate_xml:
                mImageView.startAnimation(AnimationUtils.loadAnimation(this, R.anim.rotate));
                return;
            case R.id.btn_scale_xml:
                mImageView.startAnimation(AnimationUtils.loadAnimation(this, R.anim.scale));
                return;
            case R.id.btn_translation_xml:
                mImageView.startAnimation(AnimationUtils.loadAnimation(this, R.anim.translate));
                return;
            case R.id.btn_set_xml:
                mImageView.startAnimation(AnimationUtils.loadAnimation(this, R.anim.set));
                return;
            case R.id.btn_alpha:
                animation = new AlphaAnimation(1.0f, 0f);
                break;
            case R.id.btn_rotate:
                animation = new RotateAnimation(0f, 360f, Animation.RELATIVE_TO_PARENT, 0.0f,
                        Animation.RELATIVE_TO_PARENT, 0.0f);
                break;
            case R.id.btn_scale:
                animation = new ScaleAnimation(0.0f,  1.0f,  0f,   1.0f, Animation.RELATIVE_TO_PARENT,
                        0.5f, Animation.RELATIVE_TO_PARENT, 0.5f);
                break;
            case R.id.btn_translation:
                animation = new TranslateAnimation(Animation.RELATIVE_TO_PARENT, 0f, Animation
                        .RELATIVE_TO_PARENT, 0.5f, Animation.RELATIVE_TO_SELF, 0f, Animation
                        .RELATIVE_TO_SELF, 5f);
                break;
            case R.id.btn_set:
                //定义动画集合
                AnimationSet animationSet = new AnimationSet(true);
                //定义各种希望动画集合中放置的动画
                Animation animation_alpha = new AlphaAnimation(1.0f, 0f);
                animation_alpha.setRepeatCount(Animation.INFINITE);
                Animation animation_rotate = new RotateAnimation(0f, 360f, Animation
                        .RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
                animation_rotate.setRepeatCount(Animation.INFINITE);
                Animation animation_scale = new ScaleAnimation(2.0f, 0f, 2.0f, 0f, Animation
                        .RELATIVE_TO_PARENT, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
                animation_scale.setRepeatCount(Animation.INFINITE);
                Animation animation_translate = new TranslateAnimation(Animation
                        .RELATIVE_TO_PARENT, 0f, Animation.RELATIVE_TO_PARENT, 0.5f, Animation
                        .RELATIVE_TO_SELF, 0f, Animation.RELATIVE_TO_SELF, 5f);
                animation_translate.setRepeatCount(Animation.INFINITE);

                animationSet.addAnimation(animation_alpha);
                animationSet.addAnimation(animation_rotate);
                animationSet.addAnimation(animation_scale);
                animationSet.addAnimation(animation_translate);
                animationSet.setInterpolator(new AccelerateDecelerateInterpolator());
                animationSet.setRepeatCount(Animation.INFINITE);
                animationSet.setRepeatMode(Animation.REVERSE);
                animationSet.setDuration(3000);
                mImageView.startAnimation(animationSet);
                return;
        }
        animation.setDuration(3000);
        animation.setRepeatMode(Animation.REVERSE);
        animation.setRepeatCount(Animation.INFINITE);
        //添加监听器
        animation.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {
                Log.i("TweenActivity", "---onAnimationStart()--->" + Thread.currentThread()
                        .getName());
            }
            @Override
            public void onAnimationEnd(Animation animation) {
                Log.i("TweenActivity", "---onAnimationEnd()--->" + Thread.currentThread().getName
                        ());
            }
            @Override
            public void onAnimationRepeat(Animation animation) {
                Log.i("TweenActivity", "---onAnimationRepeat()--->" + Thread.currentThread()
                        .getName());
            }
        });
        mImageView.startAnimation(animation);
    }
}
